# # -*- coding: utf-8 -*-
# # Date: 2025/6/22 14:59
#
#
# import asyncio
# import aiohttp
# import os
#
# async def download_image(session, url, save_path):
#     try:
#         async with session.get(url) as response:
#             if response.status == 200:
#                 with open(save_path, 'wb') as f:
#                     while True:
#                         chunk = await response.content.read(1024)
#                         if not chunk:
#                             break
#                         f.write(chunk)
#                 print(f"Image downloaded and saved to {save_path}")
#             else:
#                 print(f"Failed to download image from {url}. Status code: {response.status}")
#     except Exception as e:
#         print(f"Error downloading {url}: {e}")
#
# async def main(image_urls, save_folder='images'):
#     # 创建保存图片的文件夹
#     if not os.path.exists(save_folder):
#         os.makedirs(save_folder)
#
#     async with aiohttp.ClientSession() as session:
#         tasks = []
#         for i, url in enumerate(image_urls):
#             save_path = os.path.join(save_folder, f'image_{i}.jpg')
#             task = asyncio.create_task(download_image(session, url, save_path))
#             tasks.append(task)
#
#         # 等待所有任务完成
#         await asyncio.gather(*tasks)
#
# # 示例图片URL列表
# image_urls = [
#     'https://example.com/image1.jpg',
#     'https://example.com/image2.jpg',
#     'https://example.com/image3.jpg',
#     # 添加更多图片URL
# ]
#
# # 运行主函数
# asyncio.run(main(image_urls))
